package com.woniuxy.core.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.core.mapper.UserCouponMapper;
import com.woniuxy.core.service.CouponService;
import com.woniuxy.core.service.UserCouponService;
import com.woniuxy.entity.Coupon;
import com.woniuxy.entity.DTO.UserCouponDTO;
import com.woniuxy.entity.UserCoupon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserCouponServiceImpl extends ServiceImpl<UserCouponMapper, UserCoupon> implements UserCouponService {

    private final CouponService couponService;

    @Autowired
    public UserCouponServiceImpl(@Lazy CouponService couponService) {
        this.couponService = couponService;
    }

    @Override
    public List<UserCoupon> getUserCouponsByUserId(Integer userId) {
        return baseMapper.selectByUserId(userId);
    }

    @Override
    public List<UserCouponDTO> getUserCouponDTOsByUserId(Integer userId) {
        List<UserCoupon> userCoupons = baseMapper.selectByUserId(userId);
        return userCoupons.stream().map(uc -> {
            Coupon coupon = couponService.getById(uc.getCouponId());
            return new UserCouponDTO(
                    uc.getId(),
                    uc.getUserId(),
                    uc.getCouponId(),
                    uc.getStatus(),
                    uc.getCreateTime(),
                    uc.getUseTime(),
                    coupon
            );
        }).collect(Collectors.toList());
    }
}
